package com.fa.dv.controller;

import com.alibaba.excel.EasyExcel;
import com.fa.common.core.controller.BaseController;
import com.fa.common.core.domain.AjaxResult;
import com.fa.common.core.page.TableDataInfo;
import com.fa.dv.domain.Machinery;
import com.fa.dv.domain.Subject;
import com.fa.dv.domain.Workshop;
import com.fa.dv.domain.dto.SubjectDTO;
import com.fa.dv.listener.MachineryReadListener;
import com.fa.dv.listener.SubjectReadListener;
import com.fa.dv.service.IMachineryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/dv/machinery")
public class MachineryController extends BaseController {

    @Autowired
    private IMachineryService machineryService;

    @GetMapping("/list")
    public TableDataInfo list(Machinery machinery){
        List<Machinery> list = machineryService.list(machinery);
        return getDataTable(list);
    }

    @GetMapping("/workshop")
    public AjaxResult workshop(){
        List<Workshop> list = machineryService.getWorkshop();
        return AjaxResult.success(list);
    }

    @PostMapping("/insert")
    public AjaxResult insert(@RequestBody  Machinery machinery){
        machineryService.insert(machinery);
        return AjaxResult.success();
    }


    @GetMapping("/getMachineryTypeNameById")
    public AjaxResult getMachineryTypeNameById(@RequestParam Long machineryId){
        System.out.println("这是需要查询的ID======="+ machineryId);
        Machinery machinery = machineryService.getMachineryTypeNameById(machineryId);
        System.out.println("这是根据ID查询的设备类型======="+ machinery);
        return AjaxResult.success(machinery);
    }

    @GetMapping("/change")
    public AjaxResult update(Machinery machinery){
       machineryService.update(machinery);
        return AjaxResult.success();
    }

    @GetMapping("/delete")
    public AjaxResult delete(@RequestParam Long machineryId){
        machineryService.delete(machineryId);
        return AjaxResult.success();
    }

    /**
     * 导出模板
     * @param response
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws Exception {
        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("设备台账模板", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        ArrayList<Machinery> list = new ArrayList<>();
        EasyExcel.write(response.getOutputStream(), Machinery.class).sheet("模板").doWrite(list);
    }


    @PostMapping("/export")
    public void exportUnit(Machinery machinery, HttpServletResponse response) throws Exception {

        // 查询列表 //
        List<Machinery> list = machineryService.list(machinery);

        // 导出.xlsx用这个
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码
        String fileName = URLEncoder.encode("设备台账列表", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");

        EasyExcel.write(response.getOutputStream(), Machinery.class).sheet("设备台账列表").doWrite(list);
    }

    /**
     * 导入单位数据
     */
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws IOException {
        EasyExcel.read(file.getInputStream(),
                Machinery.class,
                new MachineryReadListener(machineryService)).sheet().doRead();
        return AjaxResult.success();
    }

}
